﻿using AutoMapper;
using Factory.MasterData.Materials.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Factory.MasterData.Materials
{
    /// <summary>
    /// 物料服务
    /// </summary>
    public class MaterialAppService : ApplicationService,IMaterialAppService
    {
        /// <summary>
        /// 物料仓储
        /// </summary>
        private readonly IRepository<Material, int> _materialrepository;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper _mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="materialrepository">物料仓储</param>
        /// <param name="mapper">映射</param>
        public MaterialAppService(IRepository<Material, int> materialrepository, IMapper mapper)
        {
            _materialrepository = materialrepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 显示物料信息
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="materialCode">物料编码</param>
        /// <param name="materialName">物料名称</param>
        /// <param name="materialClassificationId">物料/产品</param>
        /// <returns>返回结果</returns>
        public async Task<PageResult> GetMaterialList(int pageIndex, int pageSize, string? materialCode, string? materialName, int? materialClassificationId)
        {

            var list = await _materialrepository.GetListAsync(m=>(string.IsNullOrEmpty(materialCode)|| m.MaterialCode.Contains(materialCode!)) &&(string.IsNullOrEmpty(materialName)||m.MaterialName.Contains(materialName!)) && (m.MaterialClassificationId == materialClassificationId));
            List<MaterialDto> listDto = _mapper.Map<List<MaterialDto>>(list);
            PageResult pageResult = new PageResult();
            pageResult.RowCount = listDto.Count();
            pageResult.PageCount = (int)Math.Ceiling(pageResult.RowCount * 1.0 / pageSize);
            pageResult.Queryable = listDto.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageResult;

        }
        /// <summary>
        /// 删除物料信息
        /// </summary>
        /// <param name="materialCodes">要删除的物料编码</param>
        /// <returns>返回结果</returns>
        public async Task<int> DeletedMaterial(string materialCodes)
        {
            var materialCode = materialCodes.Split(',');
            foreach (var item in materialCode)
            {
                await _materialrepository.DeleteAsync(m=>m.MaterialCode== item);
            }
            return 1;
        }
        /// <summary>
        /// 添加物料信息
        /// </summary>
        /// <param name="materialDto">要添加的物料编码</param>
        /// <returns>返回结果</returns>
        public async Task<int> AddMaterial(MaterialDto materialDto)
        {
            Material material = _mapper.Map<Material>(materialDto);
            await _materialrepository.InsertAsync(material);
            return 1;
        }
        /// <summary>
        /// 编辑物料信息
        /// </summary>
        /// <param name="materialDto">要编辑的物料编码</param>
        /// <returns>返回结果</returns>
        public async Task<int> UpdateMaterial(MaterialDto materialDto)
        {
            Material material = _mapper.Map<Material>(materialDto);
            await _materialrepository.UpdateAsync(material, false);
            return 1;
        }

    }
}
